import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:lz_app_core/constants/app_manager.dart';
import 'package:lz_base/kd_base.dart';
import 'package:lz_ui/kd_ui.dart';
import '../serve/models/kd_mine_item.dart';
import 'package:lz_form/kd_form.dart';
import 'package:lz_base/src/widgets/kd_update_app.dart';
import 'package:rxdart/rxdart.dart';
import 'package:flutter_ume/flutter_ume.dart';
import 'package:flutter_ume/flutter_ume.dart';
import 'package:lz_app_core/utils/kd_storage.dart';

class KDAboutPage extends StatefulWidget {
  const KDAboutPage({super.key});

  @override
  State<KDAboutPage> createState() => _KDAboutPageState();
}

class _KDAboutPageState extends State<KDAboutPage> {
  late List<KDFormTitleWidgetVM> items = [];
  // StreamController debugController =

  BehaviorSubject<bool> debugStream = BehaviorSubject.seeded(!(AppManager.isRelease ?? true));

  @override
  void initState() {
    super.initState();

    if (KDUserPermission.check("check:version")) {
      items.add(
        KDFormTitleWidgetVM(
          title: "检查新版本",
          showArrowRight: true,
          isBLine: true,
          didClick: () {
            KDUpdateAppWidget.check().then((value) {
              if (value == null) return;
              if (value) {
                DialogUtils.showToast("已经是最新版本");
              }
            });
          },
        ),
      );
    }
    items.addAll([
      KDFormTitleWidgetVM(
        title: "用户协议",
        showArrowRight: true,
        isBLine: true,
        didClick: userProtocal,
      ),
      KDFormTitleWidgetVM(
        title: "隐私政策",
        showArrowRight: true,
        isBLine: true,
        didClick: priveProtocal,
      ),
      // KDFormTitleWidgetVM(
      //   title: "营业执照",
      //   showArrowRight: true,
      //   isBLine: true,
      //   didClick: () {},
      // ),
    ]);
  }

  @override
  Widget build(BuildContext context) {
    return KDScaffold.scaffold(
        title: "关于蓝智",
        body: Column(
          children: [
            SizedBox(
              height: 178,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  const Image(image: AssetImage('images/icon_ablout_logo.png')),
                  Text(
                    AppManager.appName.appName,
                    style: TextStyle(fontSize: 20, color: KDColor.title),
                  ),
                  const SizedBox(height: 4),
                  StreamBuilder(
                    stream: debugStream.stream,
                    builder: ((context, snapshot) =>
                        KDButton.border(
                            title: "v${AppManager.configModel.appVersion}",
                            borderWidth: 0,
                            bgColor: (snapshot.requireData ?? true) ? KDColor
                                .main : Colors.transparent,
                            tap: () {
                              AppManager.isRelease = !debugStream.value;
                              KDStorage.saveBool(KDStorageKey.debug.name, AppManager.isRelease);
                              debugStream.add(AppManager.isRelease);
                            },
                            width: KD.screenW / 2,
                            height: KD.size(80.0)
                        )),
                  ),
                ],
              ),
            ),
            Expanded(
              child: ListView.builder(
                  itemCount: items.length,
                  itemBuilder: ((context, index) {
                    return KDFormTitleWidget(vm: items[index]);
                  })),
            )
          ],
        ));
  }

  /// 用户协议
  userProtocal() {
    KDWebViewPage page = KDWebViewPage.webPage(
        title: "用户服务协议", url: AppManager.webUrlModel.userlPrototal);
    Get.to(() => page);
  }

  /// 隐私协议
  priveProtocal() {
    KDWebViewPage page = KDWebViewPage.webPage(
        title: "隐私政策", url: AppManager.webUrlModel.priveProtocal);
    Get.to(() => page);
  }
}
